package com.it.controller;

import com.it.pojo.PageResult;
import com.it.pojo.Result;
import com.it.pojo.Student;
import com.it.pojo.StudentQueryParam;
import com.it.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询
     * @param studentQueryParam
     * @return
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log .info("查询请求参数： {}",studentQueryParam);
        PageResult pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增学生
     */
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("新增学生，student：{}", student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 根据ID查询学生数据(用于查询回显)
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("查询学生ID：{}",id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }

    /**
     * 修改学生
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学生，student：{}", student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 批量删除学生
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,@PathVariable Integer score){
        log.info("学生ID：{}",id);
        log.info("学生扣分：{}",score);
        studentService.violation(id,score);
        return Result.success();
    }


}
